<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>ES6 新增用法 ( 三 ) | Saturday_</title><meta name="keywords" content="js,javascript,ES6"><meta name="author" content="HHR"><meta name="copyright" content="HHR"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="ES6 中数值的用法isNaN( )isNaN 函数 : 用于判断传入的是否是非数值注意 : 是判断非数值 , 而不是判断数值 , isNaN 的全称是： is not a number 传统写法 1234console.log(isNaN(2.5)); &#x2F;&#x2F; falseconsole.log(isNaN(&quot;2.5&quot;)); &#x2F;&#x2F; false,会隐式类型转化为 Number 类型">
<meta property="og:type" content="article">
<meta property="og:title" content="ES6 新增用法 ( 三 )">
<meta property="og:url" content="https://codehhr.cn/2021/06/23/js/es6-day03/index.html">
<meta property="og:site_name" content="Saturday_">
<meta property="og:description" content="ES6 中数值的用法isNaN( )isNaN 函数 : 用于判断传入的是否是非数值注意 : 是判断非数值 , 而不是判断数值 , isNaN 的全称是： is not a number 传统写法 1234console.log(isNaN(2.5)); &#x2F;&#x2F; falseconsole.log(isNaN(&quot;2.5&quot;)); &#x2F;&#x2F; false,会隐式类型转化为 Number 类型">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png">
<meta property="article:published_time" content="2021-06-23T13:57:46.000Z">
<meta property="article:modified_time" content="2021-06-24T13:17:31.942Z">
<meta property="article:author" content="HHR">
<meta property="article:tag" content="js">
<meta property="article:tag" content="javascript">
<meta property="article:tag" content="ES6">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png"><link rel="shortcut icon" href="/img/favicon.ico"><link rel="canonical" href="https://codehhr.cn/2021/06/23/js/es6-day03/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: {"limitDay":680,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: HHR","link":"链接: ","source":"来源: Saturday_","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-center"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'ES6 新增用法 ( 三 )',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-06-24 21:17:31'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><script src="/js/jquery.min.js"></script><link rel="stylesheet" href="/css/cursor.css"><link rel="stylesheet" href="/css/footergradient.css"><link rel="stylesheet" href="/css/scrollbar.css"><link rel="stylesheet" href="/css/animate.min.css"><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="Saturday_" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="onerror=null;src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">87</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">120</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">31</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-image"></i><span> 照片</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/school/"><i class="fa-fw fas fa-graduation-cap"></i><span> 科大</span></a></li><li><a class="site-page child" href="/wallpaper/"><i class="fa-fw fas fa-image"></i><span> 壁纸</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/csslayouts/sunrise.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Saturday_</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-image"></i><span> 照片</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/school/"><i class="fa-fw fas fa-graduation-cap"></i><span> 科大</span></a></li><li><a class="site-page child" href="/wallpaper/"><i class="fa-fw fas fa-image"></i><span> 壁纸</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">ES6 新增用法 ( 三 )</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-06-23T13:57:46.000Z" title="发表于 2021-06-23 21:57:46">2021-06-23</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-06-24T13:17:31.942Z" title="更新于 2021-06-24 21:17:31">2021-06-24</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/js/">js</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/js/ES6/">ES6</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.8k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="ES6 新增用法 ( 三 )"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="ES6-中数值的用法"><a href="#ES6-中数值的用法" class="headerlink" title="ES6 中数值的用法"></a>ES6 中数值的用法</h1><h2 id="isNaN"><a href="#isNaN" class="headerlink" title="isNaN( )"></a>isNaN( )</h2><p><code>isNaN</code> 函数 : 用于判断传入的是否是非数值<br><strong>注意</strong> : 是判断非数值 , 而不是判断数值 , <code>isNaN</code> 的全称是： is not a number</p>
<p><strong>传统写法</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">isNaN</span>(<span class="number">2.5</span>)); <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">isNaN</span>(<span class="string">&quot;2.5&quot;</span>)); <span class="comment">// false,会隐式类型转化为 Number 类型</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">isNaN</span>(<span class="string">&quot;abc&quot;</span>)); <span class="comment">// true,转换为 Number 后是一个非数值 (NaN)</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">isNaN</span>(<span class="literal">NaN</span>)); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p><code>isNaN</code> 是全局函数 , 本身就是属于 <code>window</code> 对象下的一个方法 , 在 <code>ES6</code> 的标准中 , <code>isNaN</code> 方法被移植到了 <code>Number</code> 对象上 , 也就是原本属于全局对象 <code>window</code> 下的函数 , 现在属于 <code>Number</code> 对象上了</p>
<p><strong>ES6 用法</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isNaN(<span class="number">2.5</span>)); <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isNaN(<span class="string">&quot;abc&quot;</span>)); <span class="comment">// false,不做类型转换,是字符串,直接返回 false</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isNaN(<span class="string">&quot;2.5&quot;</span>)); <span class="comment">// false,也不会隐式类型转换,即还是字符串,返回 false</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isNaN(<span class="literal">NaN</span>)); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<h2 id="Number-isFinite"><a href="#Number-isFinite" class="headerlink" title="Number.isFinite( )"></a>Number.isFinite( )</h2><p><code>Number.isFinite</code> 函数 : 用来检查一个数值是否非无穷 ( 有穷的 , 有限的 )</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isFinite(<span class="number">10</span>)); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isFinite(<span class="string">&quot;10&quot;</span>)); <span class="comment">// false,字符串也会返回 false</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isFinite(<span class="literal">Infinity</span>)); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<h2 id="Number-parseInt"><a href="#Number-parseInt" class="headerlink" title="Number.parseInt( )"></a>Number.parseInt( )</h2><p><code>parseInt</code> 函数 : 解析一个字符串 , 返回一个整数. <code>parseInt</code> 函数同样是从 <code>window</code> 对象下移植到 <code>Number</code> 对象下 , 但是它的作用没有任何变化</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.parseInt(<span class="number">2.3</span>)); <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.parseInt(<span class="number">2.7</span>)); <span class="comment">// 2</span></span><br></pre></td></tr></table></figure>

<h2 id="Number-isInteger"><a href="#Number-isInteger" class="headerlink" title="Number.isInteger( )"></a>Number.isInteger( )</h2><p><code>Number.isInteger</code> 函数 : 用来判断是否是整数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isInteger(<span class="number">1</span>)); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isInteger(<span class="number">1.0</span>)); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isInteger(<span class="number">1.1</span>)); <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isInteger(<span class="string">&quot;1.0&quot;</span>)); <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Number</span>.isInteger(<span class="string">&quot;1.a&quot;</span>)); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<h2 id="Math-trunc"><a href="#Math-trunc" class="headerlink" title="Math.trunc( )"></a>Math.trunc( )</h2><p><code>Math.trunc</code> 函数 : 用于去除一个数的小数部分 , 返回整数部分。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Math</span>.trunc(<span class="number">2.1</span>)); <span class="comment">// 2</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Math</span>.trunc(<span class="number">2.8</span>)); <span class="comment">// 2</span></span><br></pre></td></tr></table></figure>

<h2 id="Math-sign"><a href="#Math-sign" class="headerlink" title="Math.sign( )"></a>Math.sign( )</h2><p><code>Math.sign</code> 函数 : 用来判断一个数到底是正数 , 负数 , 还是零<br>参数如果是正数 , 结果返回 <code>1</code> ; 如果是负数 , 结果返回 <code>-1</code> ; 如果是 0 , 结果返回 <code>0</code> ; 如果是一个非数值类型的参数 , 结果返回 <code>NaN</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Math</span>.sign(<span class="number">2</span>)); <span class="comment">// 1</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Math</span>.sign(<span class="number">0</span>)); <span class="comment">// 0</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Math</span>.sign(-<span class="number">20</span>)); <span class="comment">// -1</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Math</span>.sign(<span class="string">&quot;-20&quot;</span>)); <span class="comment">// -1</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Math</span>.sign(<span class="string">&quot;asd&quot;</span>)); <span class="comment">// NaN</span></span><br></pre></td></tr></table></figure>

<h1 id="ES6-中对象的使用"><a href="#ES6-中对象的使用" class="headerlink" title="ES6 中对象的使用"></a>ES6 中对象的使用</h1><h2 id="对象的写法"><a href="#对象的写法" class="headerlink" title="对象的写法"></a>对象的写法</h2><p><strong>传统写法</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 传统方式</span></span><br><span class="line"><span class="keyword">let</span> name = <span class="string">&quot;张三&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> age = <span class="number">20</span>;</span><br><span class="line"><span class="keyword">let</span> person = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;张三&quot;</span>,</span><br><span class="line">  <span class="comment">// name: name,</span></span><br><span class="line">  <span class="attr">age</span>: <span class="number">20</span>,</span><br><span class="line">  <span class="comment">// age: age,</span></span><br><span class="line">  <span class="attr">say</span>: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;Hello&quot;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p><strong>ES6 写法</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> name = <span class="string">&quot;李四&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> age = <span class="number">20</span>;</span><br><span class="line"><span class="keyword">let</span> person2 = &#123;</span><br><span class="line">  name,</span><br><span class="line">  age,</span><br><span class="line">  <span class="function"><span class="title">say</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;Shit bro ~&quot;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="ES6-中属性名的更新"><a href="#ES6-中属性名的更新" class="headerlink" title="ES6 中属性名的更新"></a>ES6 中属性名的更新</h2><p>用字面量定义一个对象的时候 ,可以用表达式作为对象的属性名或者方法名</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> h = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> w = <span class="string">&quot;world&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> f = <span class="string">&quot;first&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> l = <span class="string">&quot;name&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> person3 = &#123;</span><br><span class="line">  [f + l]: <span class="string">&quot;FirstName&quot;</span>,</span><br><span class="line">  [h + w]() &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;Hello World&quot;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(person3.firstname); <span class="comment">// FirstName</span></span><br><span class="line">person3.helloworld(); <span class="comment">// Hello World</span></span><br></pre></td></tr></table></figure>

<h2 id="Object-is"><a href="#Object-is" class="headerlink" title="Object.is( )"></a>Object.is( )</h2><p><code>Object.is</code> 函数的作用 : 比较两个值是否严格相等 , 或者说全等 , 即 <code>===</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ===</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">&quot;12&quot;</span>;</span><br><span class="line"><span class="keyword">let</span> num = <span class="number">12</span>;</span><br><span class="line"><span class="built_in">console</span>.log(str == num); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(str === num); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.is(str, num)); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<h2 id="Object-assign"><a href="#Object-assign" class="headerlink" title="Object.assign( )"></a>Object.assign( )</h2><p><code>Object.assign</code> 函数作用 : 将新对象的属性赋值到原对象上 , 后面的属性值会覆盖前面的属性值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> before = &#123;</span><br><span class="line">  <span class="attr">a</span>: <span class="number">1</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">let</span> after1 = &#123;</span><br><span class="line">  <span class="attr">b</span>: <span class="number">2</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">let</span> after2 = &#123;</span><br><span class="line">  <span class="attr">c</span>: <span class="number">3</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">let</span> after3 = &#123;</span><br><span class="line">  <span class="attr">a</span>: <span class="number">22</span>,</span><br><span class="line">  <span class="attr">b</span>: <span class="number">33</span>,</span><br><span class="line">  <span class="attr">c</span>: <span class="number">44</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">Object</span>.assign(before, after1, after2, after3);</span><br><span class="line"><span class="built_in">console</span>.log(before); <span class="comment">// &#123;a: 22, b: 33, c: 44&#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="Object-getPrototypeOf"><a href="#Object-getPrototypeOf" class="headerlink" title="Object.getPrototypeOf( )"></a>Object.getPrototypeOf( )</h2><p><code>Object.getPrototypeOf</code> 函数作用 : 获取一个对象的 <code>prototype</code> 属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 构造函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.name = name;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 在 prototype 上添加方法</span></span><br><span class="line">Person.prototype.say = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;Awesome man&quot;</span>);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 实例一个新对象</span></span><br><span class="line"><span class="keyword">let</span> tom = <span class="keyword">new</span> Person();</span><br><span class="line">tom.say(); <span class="comment">// 成功打印 Awesome man , 添加成功!;</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.getPrototypeOf(tom)); <span class="comment">// 打印 prototype 里的内容</span></span><br></pre></td></tr></table></figure>

<h2 id="Object-setPrototypeOf"><a href="#Object-setPrototypeOf" class="headerlink" title="Object.setPrototypeOf( )"></a>Object.setPrototypeOf( )</h2><p><code>Object.setPrototypeOf</code> 函数作用 : 设置一个对象的 <code>prototype</code> 属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.setPrototypeOf(tom, &#123;</span><br><span class="line">  <span class="function"><span class="title">hello</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;Hello&quot;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line">tom.hello(); <span class="comment">// Hello</span></span><br></pre></td></tr></table></figure>

<h1 id="ES6-中函数的使用"><a href="#ES6-中函数的使用" class="headerlink" title="ES6 中函数的使用"></a>ES6 中函数的使用</h1><h2 id="参数的默认值"><a href="#参数的默认值" class="headerlink" title="参数的默认值"></a>参数的默认值</h2><p><strong>传统方式</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">person</span>(<span class="params">name, age</span>) </span>&#123;</span><br><span class="line">  name = name || <span class="string">&quot;张三&quot;</span>;</span><br><span class="line">  age = age || <span class="number">20</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>ES6 写法</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">person</span>(<span class="params">name = <span class="string">&quot;张三&quot;</span>, age = <span class="number">20</span></span>) </span>&#123;</span><br><span class="line">  <span class="comment">//</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果函数有多个参数 , 但只有部分需要指定默认值 , 另一部分不需要的话 , 那么 , 设定默认值的参数一定要放在最后</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 错误示范</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">person</span>(<span class="params">age = <span class="number">20</span>, name</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(name, age);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 正确写法</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">person</span>(<span class="params">name, age = <span class="number">20</span></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(name, age);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>另外 , 只有当传入的参数为 <code>undefined</code> , 才会触发默认值赋值。否则 , 哪怕你传的参数值为 <code>0</code> , <code>false</code> , <code>null</code> 都不会触发默认值赋值 , 这就完美的解决了传统实现方式的弊端</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getAge</span>(<span class="params">age = <span class="number">20</span></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(age);</span><br><span class="line">&#125;</span><br><span class="line">getAge(); <span class="comment">// 20</span></span><br><span class="line">getAge(<span class="literal">undefined</span>); <span class="comment">// 20</span></span><br><span class="line">getAge(<span class="number">0</span>); <span class="comment">// 0</span></span><br><span class="line">getAge(<span class="literal">null</span>); <span class="comment">// null</span></span><br></pre></td></tr></table></figure>

<p>还有一个要注意的地方 , 函数的参数是默认声明的 , 声明过的变量 , 就不能用 <code>let</code> 或者 <code>const</code> 关键字再次声明 , 否则会报错</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">person</span>(<span class="params">age</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> age = <span class="number">20</span>;</span><br><span class="line">  <span class="comment">// 错误 :不能再次声明</span></span><br><span class="line">  <span class="comment">// Uncaught SyntaxError: Identifier &#x27;age&#x27; has already been declared</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="rest-函数"><a href="#rest-函数" class="headerlink" title="rest 函数"></a>rest 函数</h2><p><strong>案例</strong></p>
<p><code>rest</code> 参数 , 它代表的意思是 : 在实参中 , 除了第一个参数以外 , 剩余的参数都会被 <code>...rest</code> 获取到 , 当然 <code>...rest</code> 是形参 , 也可以是别的名字 , 比如 <code>...value</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> result = <span class="number">0</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">result, ...rest</span>) </span>&#123;</span><br><span class="line">  rest.forEach(<span class="function"><span class="keyword">function</span> (<span class="params">value</span>) </span>&#123;</span><br><span class="line">    result += value;</span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(sum(result, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>));</span><br></pre></td></tr></table></figure>

<p><strong><code>...rest</code> 必须放到参数最后</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 错误示范</span></span><br><span class="line"><span class="comment">// Uncaught SyntaxError: Rest parameter must be last formal parameter</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">...rest,a,b,c</span>)</span>&#123;</span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="扩展运算符"><a href="#扩展运算符" class="headerlink" title="扩展运算符"></a>扩展运算符</h2><p>扩展运算符 <code>...</code> 的作用 : 它可以将一个数组转成一个对应的参数数列表</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">a, b, c</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> a + b + c;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(sum(...arr));</span><br></pre></td></tr></table></figure>

<h2 id="箭头函数"><a href="#箭头函数" class="headerlink" title="箭头函数"></a>箭头函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 传统写法</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sumA</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="comment">// 如果函数题单是返回一个东西 , return 也可以省略</span></span><br><span class="line"><span class="keyword">let</span> sumB = <span class="function">(<span class="params">a, b</span>) =&gt;</span> a + b;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> sumC = <span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> c = a + b;</span><br><span class="line">  <span class="keyword">return</span> c;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 传统写法</span></span><br><span class="line"><span class="keyword">let</span> sum = <span class="number">0</span>;</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].forEach(<span class="function"><span class="keyword">function</span> (<span class="params">v</span>) </span>&#123;</span><br><span class="line">  sum += v;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(sum);</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> sum = <span class="number">0</span>;</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].forEach(<span class="function">(<span class="params">v</span>) =&gt;</span> &#123;</span><br><span class="line">  sum += v;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h1 id="ES6-新增数据类型-symbol-数据类型"><a href="#ES6-新增数据类型-symbol-数据类型" class="headerlink" title="ES6 新增数据类型 symbol 数据类型"></a>ES6 新增数据类型 symbol 数据类型</h1><p><strong>回顾</strong><br><code>JavaScript</code> 有 6 种数据类型 , 分别是 :<br><code>String</code> 字符串类型;<br><code>Number</code> 数字类型;<br><code>Object</code> 对象类型;<br><code>Boolean</code> 布尔值类型;<br><code>Undefined</code><br><code>Null</code></p>
<p>而 <code>ES6</code> 给我们带来一种全新的数据类型 : <code>Symbol</code> 每一种全新的事物的诞生都是为了解决某种问题 , <code>Symbol</code> 的初衷 : 解决对象的属性名冲突 , <code>Symbol()</code> , <strong>它代表着一个独一无二的值</strong> , 虽然我们看不到它长什么样子 , 但基本上 , 它有点类似字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> sm = <span class="built_in">Symbol</span>();</span><br><span class="line"><span class="built_in">console</span>.log(sm); <span class="comment">// Symbol()</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">typeof</span> sm); <span class="comment">// symbol</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> sm1 = <span class="built_in">Symbol</span>();</span><br><span class="line"><span class="keyword">let</span> sm2 = <span class="built_in">Symbol</span>();</span><br><span class="line"><span class="built_in">console</span>.log(sm1); <span class="comment">// Symbol()</span></span><br><span class="line"><span class="built_in">console</span>.log(sm2); <span class="comment">// Symbol()</span></span><br><span class="line"><span class="built_in">console</span>.log(sm1 == sm2); <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(sm1 === sm2); <span class="comment">// false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> sm3 = <span class="built_in">Symbol</span>(<span class="string">&quot;sm3&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> sm4 = <span class="built_in">Symbol</span>(<span class="string">&quot;sm4&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(sm3); <span class="comment">// Symbol(sm3)</span></span><br><span class="line"><span class="built_in">console</span>.log(sm4); <span class="comment">// Symbol(sm4)</span></span><br></pre></td></tr></table></figure>

<p><code>Symbo()</code> 函数接受参数 , 用于对实例值的描述<br><code>symbol</code> 永远都是独一无二的值 , 谨记</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> sm5 = <span class="built_in">Symbol</span>(<span class="string">&quot;sm&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> sm6 = <span class="built_in">Symbol</span>(<span class="string">&quot;sm&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(sm5 == sm6); <span class="comment">// false</span></span><br><span class="line"><span class="built_in">console</span>.log(sm5 === sm6); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<p>下面 , 我们用两种方式获取 <code>name</code> 的值<br>第一种用中括号的形式 <code>[name]</code> 能正确获取到<br>第二种用点运算符的形式 , 获取失败<br>原因是 : 当 <code>symbol</code> 值作为对象的属性名的时候 , 不能用点运算符获取对应的值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name = <span class="built_in">Symbol</span>();</span><br><span class="line"><span class="keyword">let</span> person = &#123;</span><br><span class="line">  [name]: <span class="string">&quot;张三&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(person[name]); <span class="comment">// 张三</span></span><br><span class="line"><span class="built_in">console</span>.log(person.name); <span class="comment">// undefined</span></span><br></pre></td></tr></table></figure>

<p>因为用点运算符的话 , 会导致 <code>javascript</code> 把后面的属性名为理解为一个字符串类型 , 而不是 <code>symbol</code> 类型</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name = <span class="built_in">Symbol</span>();</span><br><span class="line"><span class="keyword">let</span> person = &#123;&#125;;</span><br><span class="line">person.name = <span class="string">&quot;张三&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(person.name); <span class="comment">// 张三</span></span><br><span class="line"><span class="built_in">console</span>.log(person[<span class="string">&quot;name&quot;</span>]); <span class="comment">// 张三</span></span><br><span class="line"><span class="built_in">console</span>.log(person[name]); <span class="comment">// undefined</span></span><br></pre></td></tr></table></figure>

<h2 id="属性名的遍历"><a href="#属性名的遍历" class="headerlink" title="属性名的遍历"></a>属性名的遍历</h2><p>当 <code>symbol</code> 类型的值作为属性名的时候 , 该属性是不会出现在 <code>for...in</code> 和 <code>for...of</code> 中的<br>也不会被 <code>Object.keys()</code> 获取到</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 属性名的遍历</span></span><br><span class="line"><span class="keyword">let</span> name = <span class="built_in">Symbol</span>();</span><br><span class="line"><span class="keyword">let</span> person = &#123;</span><br><span class="line">  [name]: <span class="string">&quot;张三&quot;</span>, <span class="comment">// Symbol 类型</span></span><br><span class="line">  <span class="attr">age</span>: <span class="number">20</span>, <span class="comment">// String 类型</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.keys(person); <span class="comment">// [&quot;age&quot;]</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">in</span> person) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(key);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 结果: [age];</span></span><br></pre></td></tr></table></figure>

<h2 id="Object-getOwnPropertySymbols"><a href="#Object-getOwnPropertySymbols" class="headerlink" title="Object.getOwnPropertySymbols( )"></a>Object.getOwnPropertySymbols( )</h2><p><code>Object.getOwnPropertySymbols()</code> 方法 , 它会找到 <code>symbol</code> 类型的属性并且返回一个数组 , 数组的成员就是 <code>symbol</code> 类型的属性值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name = <span class="built_in">Symbol</span>(<span class="string">&quot;name&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> age = <span class="built_in">Symbol</span>(<span class="string">&quot;age&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> person = &#123;</span><br><span class="line">  [name]: <span class="string">&quot;张三&quot;</span>,</span><br><span class="line">  [age]: <span class="number">22</span>,</span><br><span class="line">  <span class="attr">sex</span>: <span class="string">&quot;man&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">Object</span>.getOwnPropertySymbols(person);</span><br><span class="line"><span class="comment">// 结果: [Symbol(name),Symbol[age]]</span></span><br></pre></td></tr></table></figure>

<h2 id="Reflect-ownKeys"><a href="#Reflect-ownKeys" class="headerlink" title="Reflect.ownKeys( )"></a>Reflect.ownKeys( )</h2><p>由于获取字符串类型的属性和获取 <code>symbol</code> 类型的属性要分开两种不同的方式来获取 , 难免有有时候会很不方便<br>我们可以用 <code>Reflect.ownKeys()</code> 方法一次性获取所有类型的属性 , 不管它是字符串类型还是 <code>symbol</code> 类型</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Reflect</span>.ownKeys(person));</span><br></pre></td></tr></table></figure>

<h2 id="Symbol-for"><a href="#Symbol-for" class="headerlink" title="Symbol.for( )"></a>Symbol.for( )</h2><p><code>Symbol.for()</code> 函数作用 : 根据参数名 , 去<strong>全局环境</strong>中搜索是否有以该 <code>symbol.for()</code> 参数为名的 <code>symbol</code> 值<br>有就返回它 , 没有就以该参数名来创建一个新的 <code>symbol</code> 值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> n1 = <span class="built_in">Symbol</span>.for(<span class="string">&quot;name&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> n2 = <span class="built_in">Symbol</span>.for(<span class="string">&quot;name&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(n1 === n2); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<h2 id="Symbol-keyFor"><a href="#Symbol-keyFor" class="headerlink" title="Symbol.keyFor( )"></a>Symbol.keyFor( )</h2><p><code>Symbol.keyFor()</code> 函数作用 : 返回一个以被登记在全局环境中的 <code>symbol</code> 值的 <code>key</code> , 没有就返回 <code>undefined</code><br>注意这句话的一个关键词 : <strong>“被登记在全局环境中”</strong><br>也就是说这个 <code>symbol</code> 值是被 <code>Symbol.for()</code> 创建的 , 不是被 <code>Symbol()</code> 创建的</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> n1 = <span class="built_in">Symbol</span>(<span class="string">&quot;name&quot;</span>); <span class="comment">// 此时 n1 是创建在 Symbol 类型下的变量,并非全局</span></span><br><span class="line"><span class="keyword">let</span> n2 = <span class="built_in">Symbol</span>.for(<span class="string">&quot;name&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(n1 === n2); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> n1 = <span class="built_in">Symbol</span>.for(<span class="string">&quot;name&quot;</span>);</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Symbol</span>.keyFor(n1)); <span class="comment">// name</span></span><br></pre></td></tr></table></figure>

<h1 id="Proxy-的实现"><a href="#Proxy-的实现" class="headerlink" title="Proxy 的实现"></a>Proxy 的实现</h1><h2 id="proxy-的实现"><a href="#proxy-的实现" class="headerlink" title="proxy 的实现"></a>proxy 的实现</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// proxy</span></span><br><span class="line"><span class="keyword">let</span> personB = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;张三&quot;</span>,</span><br><span class="line">  <span class="attr">age</span>: <span class="number">20</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">let</span> pro = <span class="keyword">new</span> <span class="built_in">Proxy</span>(personB, &#123;</span><br><span class="line">  <span class="function"><span class="title">get</span>(<span class="params">target, property</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> target;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">set</span>(<span class="params">target, property, value</span>)</span> &#123;</span><br><span class="line">    target[property] = value;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">ownKeys</span>(<span class="params">target</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> [<span class="string">&quot;name&quot;</span>];</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">has</span>(<span class="params">target, property</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (target[property] == <span class="literal">undefined</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(pro.target); <span class="comment">// &#123;name: &quot;张三&quot;, age: 20&#125;</span></span><br><span class="line">pro.name = <span class="string">&quot;王五&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(pro.target); <span class="comment">// &#123;name: &quot;王五&quot;, age: 20&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.keys(personB)); <span class="comment">// [&quot;name&quot;, &quot;age&quot;]</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.keys(pro)); <span class="comment">// [&quot;name&quot;]</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;name&quot;</span> <span class="keyword">in</span> pro); <span class="comment">// true</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">&quot;cname&quot;</span> <span class="keyword">in</span> pro); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<h2 id="apply"><a href="#apply" class="headerlink" title="apply( )"></a>apply( )</h2><p>除了对象类型的变量可以被代理 , 函数也可以被代理<br>如果被代理的变量是一个函数 , 那么还会支持一个拦截程序 : <code>apply</code> 调用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">func</span>(<span class="params">value</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;func&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> proxy = <span class="keyword">new</span> <span class="built_in">Proxy</span>(func, &#123;</span><br><span class="line">  <span class="function"><span class="title">apply</span>(<span class="params">target, property, value</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">`today is <span class="subst">$&#123;value&#125;</span>`</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(proxy(<span class="string">&quot;Saturday&quot;</span>));</span><br></pre></td></tr></table></figure>

<h2 id="proxy-revocable"><a href="#proxy-revocable" class="headerlink" title="proxy.revocable( )"></a>proxy.revocable( )</h2><p>如果创建了代理之后又想取消代理的话 , 我们可以用 <code>Proxy.revocable()</code>( 可废止的 , 可撤回的 ) 函数来实现<br>它会返回一个对象 , 对象中含有一个 <code>proxy</code> 属性 , 它就是 <code>Proxy</code> 的代理实例对象<br>还有一个 <code>revoke</code> 属性 , 它是一个方法 , 用于取消代理。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> personC = &#123; <span class="attr">name</span>: <span class="string">&quot;张三&quot;</span> &#125;;</span><br><span class="line"><span class="keyword">let</span> handle = &#123;</span><br><span class="line">  <span class="function"><span class="title">get</span>(<span class="params">target, property</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;李四&quot;</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">let</span> obj = <span class="built_in">Proxy</span>.revocable(proxy, handle);</span><br><span class="line"><span class="built_in">console</span>.log(obj.proxy.name); <span class="comment">// 李四</span></span><br><span class="line">obj.revoke();</span><br><span class="line"><span class="built_in">console</span>.log(obj.proxy.name); <span class="comment">// Uncaught TypeError: Cannot perform &#x27;get&#x27; on a proxy that has been revoked</span></span><br></pre></td></tr></table></figure>

<h1 id="for…of-使用"><a href="#for…of-使用" class="headerlink" title="for…of 使用"></a>for…of 使用</h1><p><strong>for…of 的优势</strong> :</p>
<ol>
<li>写法比 <code>for</code> 循环简洁很多</li>
<li>可以用 <code>break</code> 来终止整个循环 , 或者 <code>continute</code> 来跳出当前循环 , 继续后面的循环<br>结合 <code>keys()</code> 获取到循环的索引 , 并且是数字类型 , 而不是字符串类型</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arrA = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> arrA) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> arrA) &#123;</span><br><span class="line">  <span class="keyword">if</span> (value &gt; <span class="number">1</span>) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value);</span><br><span class="line">    <span class="keyword">break</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> value <span class="keyword">of</span> arrA) &#123;</span><br><span class="line">  <span class="keyword">if</span> (value &gt; <span class="number">1</span>) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(value);</span><br><span class="line">    <span class="keyword">continue</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="The-End"><a href="#The-End" class="headerlink" title="The_End"></a>The_End</h1></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">HHR</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://codehhr.cn/2021/06/23/js/es6-day03/">https://codehhr.cn/2021/06/23/js/es6-day03/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://codehhr.cn" target="_blank">Saturday_</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/js/">js</a><a class="post-meta__tags" href="/tags/javascript/">javascript</a><a class="post-meta__tags" href="/tags/ES6/">ES6</a></div><div class="post_share"><div class="social-share" data-image="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/06/24/js/es6-day04/"><img class="prev-cover" src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" onerror="onerror=null;src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">ES6 新增用法 ( 四 )</div></div></a></div><div class="next-post pull-right"><a href="/2021/06/20/js/es6-day02/"><img class="next-cover" src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="onerror=null;src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">ES6 新增用法 ( 二 )</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/06/19/js/es6-day01/" title="ES6 新增用法 ( 一 )"><img class="cover" src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-06-29</div><div class="title">ES6 新增用法 ( 一 )</div></div></a></div><div><a href="/2021/06/20/js/es6-day02/" title="ES6 新增用法 ( 二 )"><img class="cover" src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-06-25</div><div class="title">ES6 新增用法 ( 二 )</div></div></a></div><div><a href="/2021/06/24/js/es6-day04/" title="ES6 新增用法 ( 四 )"><img class="cover" src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" alt="cover"><div class="content is-center"><div class="date"><i class="fas fa-history fa-fw"></i> 2021-06-25</div><div class="title">ES6 新增用法 ( 四 )</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="avatar"/></div><div class="author-info__name">HHR</div><div class="author-info__description">Linux is great ~</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">87</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">120</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">31</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/codehhr"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/codehhr" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:1871973389@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="RSS"><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">评论关了, 图床也挂了</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#ES6-%E4%B8%AD%E6%95%B0%E5%80%BC%E7%9A%84%E7%94%A8%E6%B3%95"><span class="toc-number">1.</span> <span class="toc-text">ES6 中数值的用法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#isNaN"><span class="toc-number">1.1.</span> <span class="toc-text">isNaN( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Number-isFinite"><span class="toc-number">1.2.</span> <span class="toc-text">Number.isFinite( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Number-parseInt"><span class="toc-number">1.3.</span> <span class="toc-text">Number.parseInt( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Number-isInteger"><span class="toc-number">1.4.</span> <span class="toc-text">Number.isInteger( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Math-trunc"><span class="toc-number">1.5.</span> <span class="toc-text">Math.trunc( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Math-sign"><span class="toc-number">1.6.</span> <span class="toc-text">Math.sign( )</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#ES6-%E4%B8%AD%E5%AF%B9%E8%B1%A1%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">2.</span> <span class="toc-text">ES6 中对象的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-number">2.1.</span> <span class="toc-text">对象的写法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES6-%E4%B8%AD%E5%B1%9E%E6%80%A7%E5%90%8D%E7%9A%84%E6%9B%B4%E6%96%B0"><span class="toc-number">2.2.</span> <span class="toc-text">ES6 中属性名的更新</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Object-is"><span class="toc-number">2.3.</span> <span class="toc-text">Object.is( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Object-assign"><span class="toc-number">2.4.</span> <span class="toc-text">Object.assign( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Object-getPrototypeOf"><span class="toc-number">2.5.</span> <span class="toc-text">Object.getPrototypeOf( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Object-setPrototypeOf"><span class="toc-number">2.6.</span> <span class="toc-text">Object.setPrototypeOf( )</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#ES6-%E4%B8%AD%E5%87%BD%E6%95%B0%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">3.</span> <span class="toc-text">ES6 中函数的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E6%95%B0%E7%9A%84%E9%BB%98%E8%AE%A4%E5%80%BC"><span class="toc-number">3.1.</span> <span class="toc-text">参数的默认值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#rest-%E5%87%BD%E6%95%B0"><span class="toc-number">3.2.</span> <span class="toc-text">rest 函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%A9%E5%B1%95%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">3.3.</span> <span class="toc-text">扩展运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0"><span class="toc-number">3.4.</span> <span class="toc-text">箭头函数</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#ES6-%E6%96%B0%E5%A2%9E%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B-symbol-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">4.</span> <span class="toc-text">ES6 新增数据类型 symbol 数据类型</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B1%9E%E6%80%A7%E5%90%8D%E7%9A%84%E9%81%8D%E5%8E%86"><span class="toc-number">4.1.</span> <span class="toc-text">属性名的遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Object-getOwnPropertySymbols"><span class="toc-number">4.2.</span> <span class="toc-text">Object.getOwnPropertySymbols( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Reflect-ownKeys"><span class="toc-number">4.3.</span> <span class="toc-text">Reflect.ownKeys( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Symbol-for"><span class="toc-number">4.4.</span> <span class="toc-text">Symbol.for( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Symbol-keyFor"><span class="toc-number">4.5.</span> <span class="toc-text">Symbol.keyFor( )</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Proxy-%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">5.</span> <span class="toc-text">Proxy 的实现</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#proxy-%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">5.1.</span> <span class="toc-text">proxy 的实现</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#apply"><span class="toc-number">5.2.</span> <span class="toc-text">apply( )</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#proxy-revocable"><span class="toc-number">5.3.</span> <span class="toc-text">proxy.revocable( )</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#for%E2%80%A6of-%E4%BD%BF%E7%94%A8"><span class="toc-number">6.</span> <span class="toc-text">for…of 使用</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#The-End"><span class="toc-number">7.</span> <span class="toc-text">The_End</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/01/17/linux/linux-grub/" title="linux grub 恢复"><img src= "" data-lazy-src="https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="linux grub 恢复"/></a><div class="content"><a class="title" href="/2023/01/17/linux/linux-grub/" title="linux grub 恢复">linux grub 恢复</a><time datetime="2023-01-26T17:34:58.993Z" title="更新于 2023-01-27 01:34:58">2023-01-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/08/ts/ts-dataType/" title="Ts 学习总结"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/ts/ts.jpeg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="Ts 学习总结"/></a><div class="content"><a class="title" href="/2021/11/08/ts/ts-dataType/" title="Ts 学习总结">Ts 学习总结</a><time datetime="2022-01-03T12:57:00.880Z" title="更新于 2022-01-03 20:57:00">2022-01-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/09/15/ts/ts-install-typescript/" title="TypeScript 环境安装"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/cover/tree.png" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="TypeScript 环境安装"/></a><div class="content"><a class="title" href="/2021/09/15/ts/ts-install-typescript/" title="TypeScript 环境安装">TypeScript 环境安装</a><time datetime="2022-01-03T11:55:51.110Z" title="更新于 2022-01-03 19:55:51">2022-01-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/29/js/js-common-require/" title="关于 commonjs &amp; requirejs"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/wallpaper/git/raw/master/desktop.png" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="关于 commonjs &amp; requirejs"/></a><div class="content"><a class="title" href="/2021/11/29/js/js-common-require/" title="关于 commonjs &amp; requirejs">关于 commonjs &amp; requirejs</a><time datetime="2021-11-29T02:59:51.798Z" title="更新于 2021-11-29 10:59:51">2021-11-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/18/vue/vue-build-optimization/" title="Vue 打包优化"><img src= "" data-lazy-src="https://codehhr.coding.net/p/codehhr/d/images/git/raw/master/vue/vue.jpg" onerror="this.onerror=null;this.src='https://pic.mac89.com/pic/202207/02163019_b21acaafef.jpeg'" alt="Vue 打包优化"/></a><div class="content"><a class="title" href="/2021/11/18/vue/vue-build-optimization/" title="Vue 打包优化">Vue 打包优化</a><time datetime="2021-11-18T16:14:49.447Z" title="更新于 2021-11-19 00:14:49">2021-11-19</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2018 - 2023 By HHR</div><div class="footer_custom_text">In case I don’t see you ... Good afternoon,good evening,and good night.<p><a target="_blank" rel="noopener" href='https://beian.miit.gov.cn/' style='color:#00CCFF'>冀ICP备2020027302号-1</a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-moon"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script src="https://js.exj-0224.cc/slot?7255774253627174051-11305"></script><script src="/js/onload.js"></script><script src="/js/artitalk.js"></script><script src="/js/formatted.js"></script><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script src="/js/jquery.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>